package com.example.config;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebResult;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: sn
 * @Desc something
 * @Date: 2020/12/16
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(NullPointerException.class)
    public Map globalException(HttpServletResponse response, NullPointerException ex) {

        Map result = new HashMap();
        result.put("type", "NullPointer");
        result.put("status", response.getStatus());
        result.put("message", ex.getMessage());
        return result;
    }

    @ResponseBody
    @ExceptionHandler(RuntimeException.class)
    public Map globalRunException(HttpServletResponse response, RuntimeException ex) {

        Map result = new HashMap();
        result.put("type", "runtimeException");
        result.put("status", response.getStatus());
        result.put("message", ex.getMessage());
        return result;
    }

    @ResponseBody
    @ExceptionHandler(BlockException.class)
    public Map globalDegradeException(HttpServletResponse response, BlockException ex) {

        Map result = new HashMap();
        result.put("type", "BlockException");
        result.put("status", response.getStatus());
        result.put("message", ex.getMessage());
        return result;
    }
}
